#include "BearTest.h"

#include <QtTest/QtTest>
#include <QMap>
#include <QString>
#include <QVariant>

#include "TestSuite.h"
#include "Bear.h"
#include "BBObject.h"

void BearTest::addObject()
{
	Bear bear;
	BBObject obj1;
	BBObject obj2;
	BBObject obj3;

	obj1.setName( "Obj1" );
	obj2.setName( "Obj2" );
	obj3.setName( "Obj3" );

	bear.addObject( &obj1 );
	bear.addObject( &obj2 );
	bear.addObject( &obj3 );

	QCOMPARE( bear.getObjects().size(), 3 );
}

void BearTest::setAttributeValue()
{
	QString objectName1( "Obj1" );
	QString objectName2( "Obj2" );
	QString objectName3( "Obj3" );
	QString attributeName1( "Attr1" );
	QString attributeName2( "Attr2" );
	QString attributeName3( "Attr3" );
	int attributeValue1 = 13;
	double attributeValue2 = 4.5;
	QString attributeValue3( "teste" );

	BBObject obj1;
	obj1.setName( objectName1 );
	obj1.setAttributePrimitiveValue( attributeName1, attributeValue1 );

	BBObject obj2;
	obj2.setName( objectName2 );
	obj2.setAttributePrimitiveValue( attributeName2, attributeValue2 );
	
	BBObject obj3;
	obj3.setName( objectName3 );
	obj3.setAttributePrimitiveValue( attributeName3, attributeValue3 );
	
	Bear bear;
	bear.addObject( &obj1 );
	bear.addObject( &obj2 );
	bear.addObject( &obj3 );

	QCOMPARE( bear.getAttributeValue( obj1.getName(), attributeName1 ).toInt(), attributeValue1 );
	QCOMPARE( bear.getAttributeValue( obj2.getName(), attributeName2 ).toDouble(), attributeValue2 );
	QCOMPARE( bear.getAttributeValue( obj3.getName(), attributeName3 ).toString(), attributeValue3 );
}

void BearTest::setAttributeType()
{
	QString objectName1( "Obj1" );
	QString objectName2( "Obj2" );
	QString objectName3( "Obj3" );
	QString attributeName1( "Attr1" );
	QString attributeName2( "Attr2" );
	QString attributeName3( "Attr3" );
	QString attributeType1( "int" );
	QString attributeType2( "float" );
	QString attributeType3( "QString" );

	BBObject obj1;
	obj1.setName( objectName1 );
	obj1.setAttributeType( attributeName1, attributeType1 );

	BBObject obj2;
	obj2.setName( objectName2 );
	obj2.setAttributeType( attributeName2, attributeType2 );

	BBObject obj3;
	obj3.setName( objectName3 );
	obj3.setAttributeType( attributeName3, attributeType3 );

	Bear bear;
	bear.addObject( &obj1 );
	bear.addObject( &obj2 );
	bear.addObject( &obj3 );

	QCOMPARE( bear.getAttributeType( obj1.getName(), attributeName1 ), attributeType1 );
	QCOMPARE( bear.getAttributeType( obj2.getName(), attributeName2 ), attributeType2 );
	QCOMPARE( bear.getAttributeType( obj3.getName(), attributeName3 ), attributeType3 );
}

void BearTest::getObjectsInitializationOrder()
{
	BBObject obj1;
	BBObject obj2;
	BBObject obj3;
	BBObject vetObjects[3];

	obj1.setName( "Obj1" );
	obj2.setName( "Obj2" );
	obj3.setName( "Obj3" );

	Bear bear;
	bear.addObject( &obj3 );
	bear.addObject( &obj2 );
	bear.addObject( &obj1 );
	
	QListIterator<BBObject*> iteratorOrder( bear.getObjectsInitializationOrder() );

	int i = 0;
	while ( iteratorOrder.hasNext() )
	{
		vetObjects[i] = *iteratorOrder.next();
		i++;
	}
	
	QCOMPARE( vetObjects[0].getName(), obj3.getName() );
	QCOMPARE( vetObjects[1].getName(), obj2.getName() );
	QCOMPARE( vetObjects[2].getName(), obj1.getName() );
}

void BearTest::getObjectsInitializationOrderReversed()
{
	BBObject obj1;
	BBObject obj2;
	BBObject obj3;
	BBObject vetObjects[3];

	obj1.setName( "Obj1" );
	obj2.setName( "Obj2" );
	obj3.setName( "Obj3" );

	Bear bear;
	bear.addObject( &obj3 );
	bear.addObject( &obj2 );
	bear.addObject( &obj1 );

	QListIterator<BBObject*> iteratorOrder( bear.getObjectsInitializationOrderReversed() );

	int i = 0;
	while ( iteratorOrder.hasNext() )
	{
		vetObjects[i] = *iteratorOrder.next();
		i++;
	}

	QCOMPARE( vetObjects[0].getName(), obj1.getName() );
	QCOMPARE( vetObjects[1].getName(), obj2.getName() );
	QCOMPARE( vetObjects[2].getName(), obj3.getName() );
}


void BearTest::getFirstObject()
{
	BBObject obj1;
	obj1.setName( "First" );

	BBObject obj2;
	obj2.setName( "Second" );

	Bear bear;
	bear.addObject( &obj1 );
	bear.addObject( &obj2 );

	QCOMPARE( bear.getFirstObject()->getName(), obj1.getName() );
}

REGISTER_TEST_CLASS( BearTest );